package com.qiantu.phone.widget.setting;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hjq.base.BaseDialog;
import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLDeviceGroupDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.DeviceDetailChangeEvent;
import com.qiantu.phone.event.RefreshDeviceListEvent;
import com.qiantu.phone.ui.activity.DeviceSettingActivity;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class SettingStopBtn extends ShapeLinearLayout {
    private AppActivity mContext;
    private TextView btn;
    private DeviceBean mDevice;
    public SettingStopBtn(AppActivity context, DeviceBean device) {
        this(context, null, device);
    }

    public SettingStopBtn(AppActivity context, AttributeSet attrs, DeviceBean device) {
        this(context, attrs, 0, device);
    }

    public SettingStopBtn(AppActivity context, AttributeSet attrs, int defStyleAttr, DeviceBean device) {
        super(context, attrs, defStyleAttr, R.style.shadow_layout_style);
        mContext = context;
        mDevice = device;
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_12);
        layoutParams.rightMargin = getResources().getDimensionPixelSize(R.dimen.dp_12);
        layoutParams.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_24);
        setLayoutParams(layoutParams);
        setOrientation(VERTICAL);
        int paddingSize = DensityUtils.dip2px(context, 4);
        setPadding(paddingSize, paddingSize, paddingSize, paddingSize);

        LayoutInflater.from(getContext()).inflate(R.layout.device_setting_btn, this, true);
        btn = findViewById(R.id.btn);

        updateStopStartButton(!mDevice.getIsDisable());
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDevice == null) {
                    return;
                }
                boolean isDisable = mDevice.getIsDisable();
                if(mDevice.getDeviceGroup() == 1){
                    showStartDissolutionHintDialog();;
                }else{
                    if(!isDisable){
                        showStartStopHintDialog();
                    }else{
                        deviceIsDisable();
                    }
                }
            }
        });
    }

    private void updateStopStartButton(boolean isDisable){
        if(mDevice.getDeviceGroup() == 1){
            btn.setText(R.string.device_group_dissolution);
            btn.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor1));
        }else{
            if(isDisable){
                btn.setText(R.string.stop_using);
                btn.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor1));
//            btn.setBackgroundResource(R.drawable.btn_transparent_8dp);
            }else{
                btn.setText(R.string.start_using);
                btn.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
//            btn.setBackgroundResource(R.drawable.btn_theme_round_8dp);
            }
        }
    }

    private BaseDialog mConfirmCancelHintDialog;
    private ConfirmCancelHintDialog.Builder mConfirmCancelHintDialogBuilder;

    private void showStartStopHintDialog() {
        if (mConfirmCancelHintDialog == null) {
            mConfirmCancelHintDialogBuilder = new ConfirmCancelHintDialog.Builder(getContext());
            mConfirmCancelHintDialogBuilder.setRightText(R.string.common_confirm);
            mConfirmCancelHintDialog = mConfirmCancelHintDialogBuilder.create();
        }
        mConfirmCancelHintDialogBuilder.setListener(new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
                deviceIsDisable();
            }
        });
        mConfirmCancelHintDialogBuilder.setContent(mContext.getString(R.string.stop_using_hint));
        mConfirmCancelHintDialog.show();
    }

    private void deviceIsDisable() {
        if (mDevice == null) {
            return;
        }
        boolean isDisable = mDevice.getIsDisable();
        List<String> deviceSerialNoList = new ArrayList<>();
        deviceSerialNoList.add(mDevice.getDeviceSerialNo());
        LLHttpManager.deviceIsDisable(mContext, deviceSerialNoList, !isDisable, new HttpCallback<HttpData<Void>>(mContext) {
            @Override
            public void onStart(Call call) {
                mContext.showDialog(mContext.getString(R.string.setting));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                //设置成功，取反
                updateStopStartButton(isDisable);
                mDevice.setIsDisable(!isDisable);
                if(mContext instanceof DeviceSettingActivity){
                    ((DeviceSettingActivity)mContext).updateViewByIsDisable();
                }
                LLDeviceDBManager.getInstance(getContext()).updateDisable(mDevice.getDeviceSerialNo(), !isDisable);
                EventBus.getDefault().post(new RefreshDeviceListEvent(mDevice.getRoomSerialNo(), mDevice.getIsFavourite()));
                EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                mContext.toast(R.string.set_success);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                mContext.hideDialog();
            }
        });
    }

    private void showStartDissolutionHintDialog() {
        if (mConfirmCancelHintDialog == null) {
            mConfirmCancelHintDialogBuilder = new ConfirmCancelHintDialog.Builder(getContext());
            mConfirmCancelHintDialogBuilder.setRightText(R.string.common_confirm);
            mConfirmCancelHintDialog = mConfirmCancelHintDialogBuilder.create();
        }
        mConfirmCancelHintDialogBuilder.setListener(new ConfirmCancelHintDialog.OnListener() {
            @Override
            public void onLeftClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
            }

            @Override
            public void onRightClick(BaseDialog dialog) {
                mConfirmCancelHintDialogBuilder.dismiss();
                groupDissolution();
            }
        });
        mConfirmCancelHintDialogBuilder.setContent(mContext.getString(R.string.device_group_dissolution_hint));
        mConfirmCancelHintDialog.show();
    }

    private void groupDissolution() {
        if (mDevice == null) {
            return;
        }
        LLHttpManager.groupDissolution(mContext, mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<Void>>(mContext) {
            @Override
            public void onStart(Call call) {
                mContext.showDialog(mContext.getString(R.string.setting));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                LLDeviceGroupDBManager.getInstance(getContext()).removeByGroupSerialNo(mDevice.getDeviceSerialNo());
                LLDeviceDBManager.getInstance(getContext()).remove(mDevice);
                EventBus.getDefault().post(new RefreshDeviceListEvent(mDevice.getRoomSerialNo(), mDevice.getIsFavourite()));
                EventBus.getDefault().post(new DeviceDetailChangeEvent(mDevice.getDeviceSerialNo()));
                mContext.toast(R.string.delete_success);
                mContext.finish();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                mContext.hideDialog();
            }
        });
    }
}
